//
//  UITraitCollection+MKUI.m
//  MKUI
//
//  Created by 苏尚进 on 2021/7/17.
//  Copyright © 2021 moon. All rights reserved.
//

#import "UITraitCollection+MKUI.h"
#import <objc/runtime.h>

NSString *const kMKUIUserInterfaceStyleChangeNotification=@"kMKUIUserInterfaceStyleChangeNotification";
@implementation UITraitCollection(MKUI)
+ (BOOL)mk_isDarkStyle{
    BOOL isDark = NO;
    if (@available(iOS 12.0, *)) {
        isDark = ([UIScreen mainScreen].traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
    }
    return isDark;
}
@end

@implementation UIScreen(MKUITheme)
+ (void)load{
    Method originMethod = class_getInstanceMethod([UIScreen class], @selector(traitCollectionDidChange:));
    Method swizzledMethod = class_getInstanceMethod([UIScreen class], @selector(mkuitheme_traitCollectionDidChange:));
    method_exchangeImplementations(originMethod, swizzledMethod);
}
- (void)mkuitheme_traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(8.0)){
    //调用原始的traitCollectionDidChange:方法
    [self mkuitheme_traitCollectionDidChange:previousTraitCollection];
    if (@available(iOS 12.0, *)) {
        if(self.traitCollection.userInterfaceStyle!=previousTraitCollection.userInterfaceStyle){
            [[NSNotificationCenter defaultCenter] postNotificationName:kMKUIUserInterfaceStyleChangeNotification object:nil];
        }
    } else {
    }
}
@end
